package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class LendUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LendUI
{
    // instance variables - replace the example below with your own
    
    private LendCtr lCtr;
    
    public LendUI()
    {
        lCtr = new LendCtr();
    }
    
    public void start()
    {
        lendMenu();
    }
    
    public void lendMenu()
    {
        boolean exit = false;
        
        while(!exit)
        {
            int choice = writeLendMenu();
            
            if(choice == 1)
            {
                findLend();
            }
            else if(choice == 2)
            {
                createLend();
            }
            else if(choice == 3)
            {
                returnLend();
            }
            else if(choice == 4)
            {
                updateLend();
            }
            else if(choice == 5)
            {
                findAllLend();
            }
            else
            {
                exit = true;
            }
        }
    }
    
    private int writeLendMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Lend Menu ***");
        System.out.println(" (1) Show information about loan");
        System.out.println(" (2) Loan DVD");
        System.out.println(" (3) Return DVD");
        System.out.println(" (4) Update loan");
        System.out.println(" (5) List all loans");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public int inputPersonsID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Person Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    private int inputDVDsID()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            //makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Write DVD Id: ");
            
            try
            {
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }
    
    private int inputCopyID()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            //makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Write Copy Id: ");
            
            try
            {
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }
    
    public String inputPeriode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Periode: ");
        String periode = keyboard.nextLine();
        return periode;
    }
    
    public String inputDato()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Dato: ");
        String dato = keyboard.nextLine();
        return dato;
    }
    
    public void exit()
    {
        System.out.println("\n Press Enter to return");
        
        Scanner button = new Scanner(System.in);
        boolean ok = false;
        while(!ok)
        {
            String theButton = button.nextLine();
            ok = true;
        }
    }
    
    private void findLend()
    {
        int dvdID = inputDVDsID();
        int copyID = inputCopyID();
        
        ArrayList lend = lCtr.listAllLends();
        boolean found = false;
        int index = 0;
        
        while(index < lend.size() && !found)
        {
            if(dvdID == lCtr.getLend(index).getDVDID() && copyID == lCtr.getLend(index).getCopyID())
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        if(found)
        {
            System.out.println("*** Lend Info ***");
            System.out.println("Dvd Information");
            System.out.println("DVD Id : " + lCtr.getLend(index).getDVDID());
            System.out.println("Copy Id : " + lCtr.getLend(index).getCopyID());
            System.out.println("Loan Information");
            System.out.println("Periode : " + lCtr.getLend(index).getPeriode());
            System.out.println("Dato : " + lCtr.getLend(index).getDato());
        }
        else
        {
            System.out.println("There are no loaned DVD with that ID");
        }
        
        exit();
    }
    
    private void createLend()
    {
        int personID = inputPersonsID();
        int dvdID = inputDVDsID();
        int copyID = inputCopyID();
        
        ArrayList lend = lCtr.listAllLends();
        boolean found = false;
        
        for(int index = 0; index < lend.size(); index++)
        {
            if(personID == lCtr.getLend(index).getPersonID() && dvdID == lCtr.getLend(index).getDVDID() && copyID == lCtr.getLend(index).getCopyID())
            {
                found = true;
            }
        }
        
        if(found)
        {
            System.out.println("This particular copy has already been lend");
        }
        else
        {
            System.out.println("This copy is available");
            System.out.println("Enter Periode and Dato");
            String periode = inputPeriode();
            String dato = inputDato();
            
            lCtr.createLend(personID, dvdID, copyID, periode, dato);
        }
        
        exit();
    }
    
    private void returnLend()
    {
        int dvdID = inputDVDsID();
        int copyID = inputCopyID();
        
        ArrayList lend = lCtr.listAllLends();
        boolean found = false;
        
        for(int index = 0; index < lend.size(); index++)
        {
            if(dvdID == lCtr.getLend(index).getDVDID() && copyID == lCtr.getLend(index).getCopyID())
            {
                found = true;
            }
        }
        
        if(!found)
        {
            System.out.println("This copy has not been lend out");
        }
        else
        {            
            lCtr.returnLend(dvdID, copyID);
        }
        
        exit();
    }
    
    private void updateLend()
    {
        int dvdID = inputDVDsID();
        int copyID = inputCopyID();
        
        ArrayList lend = lCtr.listAllLends();
        boolean found = false;
        int index = 0;
        
        while(index < lend.size() && !found)
        {
            if(dvdID == lCtr.getLend(index).getDVDID() && copyID == lCtr.getLend(index).getCopyID())
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        if(found)
        {
            String periode = inputPeriode();
            String dato = inputDato();
            int personID = inputPersonsID();
            
            lCtr.getLend(index).setPeriode(periode);
            lCtr.getLend(index).setDato(dato);
            lCtr.getLend(index).setPersonID(personID);
        }
        else
        {
            System.out.println("There are no loans with this copy");
        }
        
        exit();
    }
    
    private void findAllLend()
    {
        for(int index = 0; index < lCtr.listAllLends().size(); index++)
        {
            System.out.println("Copy ID : " + lCtr.getLend(index).getCopyID());
            System.out.println("DVD ID: " + lCtr.getLend(index).getDVDID());
            System.out.println("Person ID : " + lCtr.getLend(index).getPersonID());
            System.out.println("Periode : " + lCtr.getLend(index).getPeriode());
            System.out.println("Dato : " + lCtr.getLend(index).getDato());
            System.out.println("*** ***");
        }
        
        exit();
    }
}
